package cn.edu.neu.softlab.jason.offer.jianzhioffer.datastructure;

import java.util.Scanner;

/**
 * 剑指offer—面试4
 * 替换空格
 * 请实现一个函数，把字符串中的每个空格替换成"%20"。例如，输入"we are happy",则输出
 * "we%20are%20happy"
 */
public class Mianshi4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        char[] tmp = str.toCharArray();
        char[] string = new char[100];
        for (int i = 0; i < tmp.length; i++) {
            string[i] = tmp[i];
        }
        string[tmp.length] = '\0';
        replaceBlank(string, 100);
    }

    public static String replaceString(String str) {
        return str.replaceAll(" ","%20");
    }

    public static void replaceBlank(char[] string, int length) {
        if (string == null && length <= 0) {
            return ;
        }

        int str_length = 0;
        int numofblank = 0;
        int i = 0;
        while (string[i] != '\0') {
            str_length++;
            if (string[i] == ' ') {
                numofblank++;
            }
            i++;
        }

        int newlength = str_length + numofblank * 2;
        if (newlength > length) {
            System.out.println("数组长度不足");
            return ;
        }

        int originalpos = str_length;
        int newpos = newlength + 1;
        while (newpos > originalpos && originalpos > 0) {
            if (string[originalpos] == ' ') {
                string[--newpos] = '0';
                string[--newpos] = '2';
                string[--newpos] = '%';
                originalpos--;
            } else {
                string[--newpos] = string[originalpos];
                originalpos--;
            }
        }

        System.out.println(string);
    }
}
